<?php
// +------------------------------------------------------------------
// | cls-captcha [ think captcha, use cache save key-value, does not use session. ]
// | Copyright (c) 2022 All rights reserved.
// | Based on think-captcha, apply to ThinkPHP 6 | 8.
// | Licensed Mulan PSL v2 ( http://license.coscl.org.cn/MulanPSL2 )
// | Author: CLS <422064377>
// | CreateDate: 2024/8/20
// +------------------------------------------------------------------


if (!function_exists('cls_captcha')) {
    /**
     * 生成验证码
     *      返回 ["key"=>"存储key","img"=>"base64图片内容"]
     *
     * @param string|null $config   验证码配置标识(默认null)
     * @param string|null $storeKey 缓存存储标识(默认null)
     *
     * @return array
     */
    function cls_captcha(
        string|null $config = null,
        string|null $storeKey = null
    ): array
    {
        if ($storeKey === null) {
            return \chleniang\ClsCaptcha\facade\Captcha::create($config);
        }
        return \chleniang\ClsCaptcha\facade\Captcha::store($storeKey)->create($config);
    }
}

if (!function_exists('cls_captcha_check')) {
    /**
     * 校验验证码是否正确
     *
     * @param string      $cacheKey 缓存存储码
     * @param string      $code     用户验证码
     * @param string|null $storeKey 缓存存储标识(默认null)
     *
     * @return bool
     */
    function cls_captcha_check(
        string      $cacheKey,
        string      $code,
        string|null $storeKey = null
    ): bool
    {
        if ($storeKey === null) {
            return \chleniang\ClsCaptcha\facade\Captcha::check($cacheKey, $code);
        }
        return \chleniang\ClsCaptcha\facade\Captcha::store($storeKey)->check($cacheKey, $code);
    }
}
